package com.example.community.dao;

import com.example.community.mapper.ParkingMapper;
import com.example.community.mapper.RoomMapper;
import com.example.community.mapper.UnitMapper;
import com.example.community.model.Parking;
import com.example.community.model.Room;
import com.example.community.model.Unit;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 房间表
 */
public class RoomDao {
    public Room fandById(Integer id){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            return mapper.selectByPrimaryKey(id);
        }
    }

    public Room fandByUsNo(Integer usNo){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            return mapper.selectUsNo(usNo);
        }
    }

    public List<Room> selectUnNo(Integer unNo){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            return mapper.selectUnNo(unNo);
        }
    }

    public List<Room> fandByAll(){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            return mapper.selectAll();
        }
    }

    public List<Room> selectNull(){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            return mapper.selectNull();
        }
    }

    public Integer update(Room room){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            int i = mapper.updateByPrimaryKey(room);
            session.commit();
            return i;
        }
    }
    public List<Room> fandLimit(Integer ys, Integer shuju){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            Integer yss =(ys - 1) * shuju;
            return mapper.selectLimit(yss,shuju);
        }
    }

    public Integer selectLimitCount(Integer pageSize){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            int count = mapper.CountRoom();
            return (count + pageSize - 1)/pageSize;
        }
    }

    public Integer insert(Room room){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            int i = mapper.insert(room);
            session.commit();
            return i;
        }
    }

    public Integer delete(Integer id){
        try (SqlSession session = MybatisUtil.getSession()){
            final RoomMapper mapper = session.getMapper(RoomMapper.class);
            int i = mapper.deleteByPrimaryKey(id);
            session.commit();
            return i;
        }
    }
}
